package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.BasicCarDetails;
import com.ruicar.afs.cloud.basic.common.entity.BasicChannelInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicContractCustRelation;
import com.ruicar.afs.cloud.basic.common.entity.BasicCustAddressDetails;
import com.ruicar.afs.cloud.basic.common.service.BasicCarDetailsService;
import com.ruicar.afs.cloud.basic.common.service.BasicChannelInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicContractCustRelationService;
import com.ruicar.afs.cloud.basic.common.service.BasicCustAddressDetailsService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.AddressTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.PersonRoleTypeEnum;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 渠道信息相关增删改查
 */
@Slf4j
@RestController
@RequestMapping("/channelInfo")
@AllArgsConstructor
public class BasicChannelInfoController {

    private BasicCarDetailsService basicCarDetailsService;
    private final BasicChannelInfoService basicChannelInfoService;
    private final BasicCustAddressDetailsService basicCustAddressDetailsService;
    private final BasicContractCustRelationService basicContractCustRelationService;

    @ApiOperation(value = "根据合同号码查询渠道信息")
    @PostMapping("/getChannelDetailInfo")
    public IResponse<BasicChannelInfo> getChannelDetailInfo(@RequestParam("contractNo") String contractNo) {
        BasicChannelInfo basicChannelInfo = basicChannelInfoService.getOne(Wrappers.<BasicChannelInfo>lambdaQuery().
                eq(BasicChannelInfo::getContractNo, contractNo));
        BasicContractCustRelation basicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery().
                eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, AfsEnumUtil.key(PersonRoleTypeEnum.MAIN_PERSON)));
        BasicCustAddressDetails basicCustAddressDetails = basicCustAddressDetailsService.getOne(Wrappers.<BasicCustAddressDetails>lambdaQuery().
                eq(BasicCustAddressDetails::getCustNo, basicContractCustRelation.getCustNo())
                .eq(BasicCustAddressDetails::getAddressType, AfsEnumUtil.key(AddressTypeEnum.HOME)));
        BasicCarDetails basicCarDetails = basicCarDetailsService.getOne(Wrappers.<BasicCarDetails>lambdaQuery().
                eq(BasicCarDetails::getContractNo, contractNo));
        basicChannelInfo.setProvince(basicCustAddressDetails.getProvince());
        basicChannelInfo.setCity(basicCustAddressDetails.getCity());
        basicChannelInfo.setModelName(basicCarDetails.getModelName());
        return IResponse.success(basicChannelInfo);
    }

}
